<?php	
//header("content-type:text/html;charset=utf8");
// 允许上传的图片后缀
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
//echo $_FILES["file"]["size"];
$extension = end($temp);     // 获取文件后缀名
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 204800)   // 小于 200 kb
&& in_array($extension, $allowedExts))
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "错误：: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
	  	$img_path=$_FILES["file"]["name"];
//  	echo $img_path;
//      echo "上传文件0名: " . $_FILES["file"]["name"] . "<br>";
//      echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
//      echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
//      echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"] . "<br>";
        
        // 判断当期目录下的 upload 目录是否存在该文件
        // 如果没有 upload 目录，你需要创建它，upload 目录权限为 777
        if (file_exists("../upload/user/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " 文件已经存在。 ";
        }
        else
        {
            // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
            move_uploaded_file($_FILES["file"]["tmp_name"],iconv("UTF-8", "gb2312", "../img/system/goods/" . $_FILES["file"]["name"]));
//          echo "文件存储在: " . "upload/" . $_FILES["file"]["name"];
        }
    }
}
else
{
    echo "非法的文件格式";
}
//echo $img_path;
	$img_pathi=$img_path;
	$username=$_POST['username'];
	$user_nickname=$_POST['user_nickname'];
	$goods_type=$_POST['goods_type'];
	$goods_name=$_POST['goods_name'];
	$goods_money=$_POST['goods_money'];
	$quality=$_POST['quality'];
	$school_address=$_POST['school_address'];
	$exchange_way=$_POST['exchange_way'];
	$number=$_POST['number'];
	$contact_phone=$_POST['contact_phone'];
	$contact_qq=$_POST['contact_qq'];
	$contact_wechat=$_POST['contact_wechat'];
	$exchange_status=$_POST['exchange_status'];	
	$db=@mysqli_connect('localhost','root','123456','db_exchange')//利用mysqli_connect()连接到数据库
	or die("无法连接到数据库");
	mysqli_query($db,"set names 'utf8'");
	$q="insert into tb_goods(username,user_nickname,img_path,goods_type,goods_name,goods_money,quality,school_address,exchange_way,number,contact_phone,contact_qq,contact_wechat,exchange_status) values ('$username','$user_nickname','$img_pathi','$goods_type','$goods_name','$goods_money','$quality','$school_address','$exchange_way','$number','$contact_phone','$contact_qq','$contact_wechat','$exchange_status')";//定义一条SQL语句，将提交到本页面的username,password数据提交到数据库ex_change中的tb_login表中
	$result=mysqli_query($db,$q);//利用mysqli_query()来执行上面的SQL文件，返回结果我true或者false，将结果返回给$result
	if($result){
	echo "<script>alert('发布成功！');history.back()</script>";
//	echo "发布成功！";
	}
	mysqli_close($db);
?>